﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class frmThietbi : PRE.Catalog.FrmBase
    {
        DAL.Entities_DAL.THIETBI_DAL _dal = new DAL.Entities_DAL.THIETBI_DAL();
        BLL.Entities_BLL.Xu_ly_chung _xlc = new BLL.Entities_BLL.Xu_ly_chung();

        public frmThietbi()
        {
            InitializeComponent();
        }
        //them
        protected override void PerformAdd()
        {           
                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    ResetText();
                    txtMatb.Focus();
              
            base.PerformAdd();
        }

        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        protected override void PerformDelete()
        {
            //click zo test de xoa
            string id = txtMatb.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtTentb.Focus();
            base.PerformEdit();
        }

        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txtMatb.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }
       
        protected override void PerformSave()
        {
            var ten = Proper(txtTentb.Text);
            
            if (IsAdd)
            {
                var o = new DAL.Entities.THIET_BI()
                {
                    MA_TB = txtMatb.Text,
                    TEN_TB = ten,
                    TRANG_THAI = cbTT.Text,
                    MA_LTB = cbLTB.EditValue + "",
                    MA_NV = cbNV.EditValue + "",
                    MA_NCC = cbNCC.EditValue + "",
                    SL_NHAP = txtSL.Text,
                    NGAY_NHAP = Convert.ToDateTime(dtNgaynhap.EditValue) + "",
                    NAM_SX = dtNam.Text,
                    SO_NAM_SD = txtSonam.Text
                };

                //them
                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            //sua
            if (IsEdit)
            {
                var _id = grvMain.GetFocusedRowCellValue("MA_NV") + "";

                var o = new DAL.Entities.THIET_BI()
                {
                    MA_TB = txtMatb.Text,
                    TEN_TB = ten,
                    TRANG_THAI = cbTT.Text,
                    MA_LTB = cbLTB.EditValue + "",
                    MA_NV = cbNV.EditValue + "",
                    MA_NCC = cbNCC.EditValue + "",
                    SL_NHAP = txtSL.Text,
                    NGAY_NHAP = Convert.ToDateTime(dtNgaynhap.EditValue) + "",
                    NAM_SX = dtNam.Text,
                    SO_NAM_SD = txtSonam.Text
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }
            base.PerformSave();
        }
       
        protected override void ResetText()
        {
            //bam nut them lam rong texbox
            txtTentb.Text = null;
            cbTT.Text = null;
            cbLTB.Text = null;
            cbNV.Text = null;
            cbNCC.Text = null;
            txtSL.Text = null;
            dtNgaynhap.Text = null;
            dtNam.Text = null;
            txtSonam.Text = null;

            base.ResetText();
        }

        protected override void ClearDataBindings()
        {
            txtMatb.DataBindings.Clear();
            txtTentb.DataBindings.Clear();
            cbTT.DataBindings.Clear();
            cbLTB.DataBindings.Clear();
            cbNV.DataBindings.Clear();
            cbNCC.DataBindings.Clear();
            txtSL.DataBindings.Clear();
            dtNgaynhap.DataBindings.Clear();
            dtNam.DataBindings.Clear();
            txtSonam.DataBindings.Clear();

            base.ClearDataBindings();
        }

        protected override void DataBindingControl()
        {
            //show len texbox
            txtMatb.DataBindings.Add("EditValue", _dtb, ".MA_TB");
            txtTentb.DataBindings.Add("EditValue", _dtb, ".TEN_TB");
            cbTT.DataBindings.Add("EditValue", _dtb, ".TRANG_THAI");
            cbLTB.DataBindings.Add("EditValue", _dtb, ".MA_LTB");
            cbNV.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            cbNCC.DataBindings.Add("EditValue", _dtb, ".MA_NCC");
            txtSL.DataBindings.Add("EditValue", _dtb, ".SL_NHAP");
            dtNgaynhap.DataBindings.Add("EditValue", _dtb, ".NGAY_NHAP");
            dtNam.DataBindings.Add("EditValue", _dtb, ".NAM_SX");
            txtSonam.DataBindings.Add("EditValue", _dtb, ".SO_NAM_SD");

            base.DataBindingControl();
        }
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //show len cho xem
            txtMatb.Properties.ReadOnly = isReadOnly;
            txtTentb.Properties.ReadOnly = isReadOnly;
            cbTT.Properties.ReadOnly = isReadOnly;
            cbLTB.Properties.ReadOnly = isReadOnly;
            cbNV.Properties.ReadOnly = isReadOnly;
            cbNCC.Properties.ReadOnly = isReadOnly;
            txtSL.Properties.ReadOnly = isReadOnly;
            dtNgaynhap.Properties.ReadOnly = isReadOnly;
            dtNam.Properties.ReadOnly = isReadOnly;
            txtSonam.Properties.ReadOnly = isReadOnly;

            dsTB.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void LoadData()
        {
            //load len luoi data
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsTB.DataSource = _dtb;
            }

            base.LoadData();
        }
      
        private void frmThietbi_Load(object sender, EventArgs e)
        {
            DataTable _dtb3 = _dal.load_LTB();
            cbLTB.Properties.DataSource = _dtb3;

            DataTable _dtb4 = _dal.load_NCC();
            cbNCC.Properties.DataSource = _dtb4;

            DataTable _dtb5 = _dal.load_NV();
            cbNV.Properties.DataSource = _dtb5;
        }

        private void cmdLoadLTB_Click(object sender, EventArgs e)
        {
            frmLTB_load _frmltb = new frmLTB_load();
            _frmltb.ShowDialog();
        }

        private void cbLTB_MouseHover(object sender, EventArgs e)
        {
            DataTable _dtb3 = _dal.load_LTB();
            cbLTB.Properties.DataSource = _dtb3;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            frmNCC_loads _frmncc = new frmNCC_loads();
            _frmncc.ShowDialog();
        }

        private void cbNCC_MouseHover(object sender, EventArgs e)
        {
            DataTable _dtb3 = _dal.load_NCC();
            cbNCC.Properties.DataSource = _dtb3;
        }

        private void cmdIn_Click(object sender, EventArgs e)
        {
            var frm = new Report.Report_Base();
            var xr = new Reports.XR_TB();
            xr.DataSource = _dal.ds_tb();
            frm.SetReport(xr);
            frm.Show();
        }
    }
}
